package com.zdsoft.finance.projectfolder.repository;


import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.zdsoft.finance.projectfolder.entity.ProjectInfo;
import com.zdsoft.finance.projectfolder.entity.ProjectInfoType;
import com.zdsoft.framework.core.common.domain.BaseEntity;
import com.zdsoft.framework.core.common.util.ObjectHelper;

public class ProjectInfoTypeRepositoryImpl {

	@PersistenceContext
	private EntityManager em;
	
	public ProjectInfoType findProjectinfoTypeByProjectinfo(String businessId,String projectCd) throws Exception{
		if (ObjectHelper.isNotEmpty(businessId)&&ObjectHelper.isNotEmpty(projectCd)){
            StringBuffer sql=new StringBuffer(" select distinct t.* from zf_project_info_type t where  t.isDeleted=:isDeleted  AND t.id=(" +
                    " select y.projectInfoType_id FROM zf_project_info y" +
                    " where y.isDeleted=:isDeleted  and y.businessId=:businessId  and y.projectCd=:projectCd )");
            Query query=em.createNativeQuery(sql.toString(),ProjectInfoType.class);
            query.setParameter("isDeleted", !BaseEntity.DELETED);
            query.setParameter("businessId", businessId);
            query.setParameter("projectCd", projectCd);
            List<ProjectInfoType> sourceData=query.getResultList();
            if(ObjectHelper.isNotEmpty(sourceData)&&sourceData.size()==1){
                return sourceData.get(0);
            }else {
                throw new Exception("此数据未进行详情配置");
            }
        }else {
            throw new Exception("项目文件夹跳转详情出错，参数为空");
        }
	}
	
	public ProjectInfo findProjectinfoBybusinessidAndProjectCd(String businessId,String projectCd) throws Exception{
		if (ObjectHelper.isNotEmpty(businessId)&&ObjectHelper.isNotEmpty(projectCd)){
            StringBuffer sql=new StringBuffer("  select distinct y.* FROM zf_project_info y" +
                    " where y.isDeleted=:isDeleted  and y.businessId=:businessId  and y.projectCd=:projectCd ");
            Query query=em.createNativeQuery(sql.toString(),ProjectInfo.class);
            query.setParameter("isDeleted",!BaseEntity.DELETED);
            query.setParameter("businessId",businessId);
            query.setParameter("projectCd",projectCd);
            ProjectInfo data=(ProjectInfo)query.getSingleResult();
            return  data;
        }else {
            throw new Exception("项目文件夹跳转详情出错，参数为空");
        }
	}
	
	public List<ProjectInfoType> showProjectFolderGroup(String projectCd,String appCd){
		if (ObjectHelper.isNotEmpty(projectCd)&&ObjectHelper.isNotEmpty(appCd)){
            StringBuffer sql=new StringBuffer("SELECT DISTINCT infoType.groupNm,infoType.infoLabel,infoType.displayOrder"+
            			" FROM zf_project_info_type infoType,zf_project_info info"+
            			" WHERE"+
            				" infoType.isDeleted = :isDeleted"+
            				" AND info.projectCd = :projectCd"+
            				" AND infoType.appCd = :appCd"+
            				" AND infoType.isFix = :isFix"+
            				" ORDER BY infoType.displayOrder ASC;");
            Query query=em.createNativeQuery(sql.toString(),ProjectInfoType.class);
            query.setParameter("isDeleted",!BaseEntity.DELETED);
            query.setParameter("projectCd",projectCd);
            query.setParameter("appCd",appCd);
            query.setParameter("isFix",false);
            List<ProjectInfoType> data=query.getResultList();
            return  data;
        }else {
        	return null;
        }
	}

	public List<ProjectInfoType> showFixProjectFolderGroup(String groupNm,String appCd){
		if (ObjectHelper.isNotEmpty(groupNm)&&ObjectHelper.isNotEmpty(appCd)){
            StringBuffer sql=new StringBuffer("SELECT DISTINCT infoType.*"+
            		" FROM"+
            		" zf_project_info_type infoType"+
            		" WHERE"+
            		" infoType.isDeleted = :isDeleted"+
            		" AND infoType.isFix = :isFix"+
            		" AND infoType.appCd = :appCd"+
            		" AND infoType.groupNm =:groupNm"+
            		" ORDER BY infoType.displayOrder ASC");
            Query query=em.createNativeQuery(sql.toString(),ProjectInfoType.class);
            query.setParameter("isDeleted",!BaseEntity.DELETED);
            query.setParameter("isFix",true);
            query.setParameter("appCd",appCd);
            query.setParameter("groupNm",groupNm);
            List<ProjectInfoType> data=query.getResultList();
            return  data;
        }else {
        	return null;
        }
	}

}
